package com.example.administrator.exkotlin.ui

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.appcompat.app.AlertDialog
import android.text.TextUtils
import android.transition.Visibility
import android.view.View
import android.view.View.VISIBLE
import com.example.administrator.exkotlin.R
import com.example.administrator.exkotlin.activity.ActivityCollector
import com.example.administrator.exkotlin.activity.BaseActivity
import com.example.administrator.exkotlin.view.TitleView
import kotlinx.android.synthetic.main.activity_normal_ui.*
import kotlinx.android.synthetic.main.title.*
import kotlinx.android.synthetic.main.title.view.*

/**
 * 常用的Ui控件
 * */
class NormalUiActivity : BaseActivity(), View.OnClickListener, TitleView.TitleEditListtner {


    private val myHandler = MyHandler()

    private lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_normal_ui)
        context = this
        initDatas()
    }

    private fun initDatas() {
        btn_save.setOnClickListener(this)
        Title.titleEditListtner = this
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_save -> {
                if (!TextUtils.isEmpty(editText.text.toString())) {
                    Title.tv_title.text = editText.text.toString()
                    pb_test.visibility = VISIBLE
                    iv_test.visibility = View.VISIBLE
                    myHandler.sendEmptyMessageDelayed(1, 200)

                }
            }
            R.id.btn_back -> {
                finish()
            }
            R.id.btn_edit -> {
                dialogShow()
            }
        }
    }

    inner class MyHandler : Handler() {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when (msg?.what) {
                1 -> {
                    myHandler.removeMessages(1)
                    if (pb_test.visibility == View.VISIBLE && pb_test.progress < pb_test.max) {
                        pb_test.progress = pb_test.progress + 10
                        myHandler.sendEmptyMessageDelayed(1, 100)
                    } else {
                        dialogShow()
                    }
                }
            }

        }
    }

    override fun editCheckListtner() {
        dialogShow()
    }

    private fun dialogShow() {
        AlertDialog.Builder(context).apply {
            setTitle("测试通知")
            setMessage("来自输入${Title.tv_title.text}")
            setCancelable(false)
            setPositiveButton("完成") { dialog, which ->
                dialog.dismiss()
                pb_test.progress = 0
            }
            setNegativeButton("退出") { dialog, which ->
                dialog.cancel()
                exsit()
            }
            show()
        }
    }
}
